/*
 * This file is part of LangMan.
 *
 * LangMan is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * LangMan is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LangMan.  If not, see <http://www.gnu.org/licenses/>.
 */

package langman.gui.profile;

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import langman.gui.StageService;
import langman.util.doping.Bean;
import langman.util.doping.Inject;
import langman.util.guifactory.AbstractFXMLBeanControl;

import java.io.File;

@Bean
public class FileSelector extends AbstractFXMLBeanControl {
    private final StageService stageService;
    @FXML
    private TextField fileNameTextField;

    @FXML
    private Button browseButton;

    private final ObjectProperty<File> selectedFile = new SimpleObjectProperty<>();

    private final ObservableList<ExtensionFilter> extensionFilters = FXCollections.observableArrayList();
    private FileSelectorMode fileSelectorMode = FileSelectorMode.File;

    @Inject
    public FileSelector(StageService stageService) {
        this.stageService = stageService;
    }

    @Override
    protected void initializeAfterInjection() {
        browseButton.setOnAction(actionEvent -> {
            if (fileSelectorMode == FileSelectorMode.File) {
                FileChooser fileChooser = new FileChooser();
                fileChooser.getExtensionFilters().setAll(extensionFilters);
                File file = fileChooser.showOpenDialog(stageService.getStage());
                selectedFile.set(file);
            } else if (fileSelectorMode == FileSelectorMode.Directory) {
                DirectoryChooser directoryChooser = new DirectoryChooser();
                File file = directoryChooser.showDialog(stageService.getStage());
                selectedFile.set(file);
            }
        });

        selectedFile.addListener((observableValue, oldValue, newValue) -> {
            if (newValue != null) {
                fileNameTextField.setText(newValue.getAbsolutePath());
            } else {
                fileNameTextField.setText("");
            }
        });
    }

    public void setExtensionFilters(ExtensionFilter... extensionFilters) {
        this.extensionFilters.setAll(extensionFilters);
    }

    public File getSelectedFile() {
        return selectedFile.get();
    }

    public ObjectProperty<File> selectedFileProperty() {
        return selectedFile;
    }

    public void setSelectedFile(File selectedFile) {
        this.selectedFile.set(selectedFile);
    }

    public void setMode(FileSelectorMode directory) {
        fileSelectorMode = directory;
    }
}
